$(document).ready(function(){
    var marks="";//检查评论
    var date = new Date();//伤害发生日期：用户可以按照伤害发生日期进行查询，必输项，默认为当天
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if (month < 10) {
        month = "0" + month;
    }
    if (day < 10) {
        day = "0" + day;
    }
    $("#tdate").val(year + "-" + month + "-" + day);
init();
    $("#tdate").change(function(){
        var tb = document.getElementById('t1');
        var rowNum=tb.rows.length;
        for (i=1;i<rowNum;i++)
        {
            tb.deleteRow(i);
            rowNum=rowNum-1;
            i=i-1;
        }

        $("#tdate").attr("value",$(this).val()); //赋值
        var date=$("#tdate").val();
        $.ajax({
            type : "get",
            url : "/lookTeamExam/commentHomework",
            dataType : "json",
            data:{
              date:date
            },
            success : function(data) {
                $("#winnerId").empty();
                for(var i in data){
                    if(data[i].score!=null&&data[i].comment!=null){
                        $("#winnerId").append("<option value=" + data[i].groupId + ">" + data[i].userNames + "</option>");
                        $("#t1").append("<tr><td>" + data[i].groupId+ "</td>" +
                            "<td>"+data[i].userNames+"</td>" +
                            "<td>"+data[i].spendTime+"</td>" +
                            "<td>"+data[i].score+"</td>" +
                            "<td >\<input type='button' class='searchHomework' value='查看'>\</td></tr>");
                    }else{
                        $("#t1").append("<tr><td>" + data[i].groupId+ "</td>" +
                            "<td>"+data[i].userNames+"</td>" +
                            "<td>"+data[i].spendTime+"</td>" +
                             "<td>" + "未评分" + "</td>" +
                            "<td >\<input type='button' class='correctHomework' value='未批改'>\</td></tr>");
                    }
                }

                $(".correctHomework").click(function(){
                    var groupId=$(this).parent().parent().find("td").eq(0).text();
                    $(this).val("提交");
                    if($(this).val()=="提交"){
                        $(this).click(function(){
                            var comment=$("#comment").val();
                            var date=$("#tdate").val();
                            var radios = document.getElementsByName("theRadio");
                            if(comment==null||comment==""){
                                alert("请填写评论");
                                return;
                            }

                            for (i = 0; i < radios.length; i++) {
                                if (radios[i].checked) {
                                    marks=1;
                                    var radioValue=radios[i].value;
                                    $.ajax({
                                        type : "POST",
                                        url : "/lookTeamExam/uploadResult/",
                                        dataType : "text",
                                        data:{
                                            comment:comment,
                                            date:date,
                                            score:radioValue,
                                            groupId:groupId
                                        },
                                        success : function(data) {
                                            alert("提交成功");
                                           if(1==data){
                                               $(this).val("查看");
                                           }
                                            console.log(data.result);
                                        },
                                        error : function(xhr,textStatus) {
                                            console.log('错误');
                                            console.log(xhr);
                                            console.log(textStatus);
                                        }
                                    });
                                    break;
                                }
                            }
                            if(marks!=1){
                                alert("请打分");
                            }
                        });
                    }

                    $.ajax({
                        type : "get",
                        url : "/lookTeamExam/searchHomework",
                        dataType : "json",
                        data:{
                            groupId:groupId,
                            date:date
                        },
                        success : function(data) {
                            $("#t2").empty();
                            for(var i in data.names){
                                $("#t2").append("<td ><input type='button' class='lookHomework' value="+data.names[i]+">"+"</td>" );
                            }
                            var len=$("input[name='theRadio']").length;
                            for(var j =0;j<len;j++){
                                if($("input[name='theRadio']:eq("+j+")").val()==data.score){
                                    $("input[name='theRadio']").get(j).checked=true;
                                }
                            }
                            $("#comment").val(data.comment);


                            $(".lookHomework").click(function(){
                                var name=$(this).val();
                                $.ajax({
                                    type : "GET",
                                    url : "/lookTeamExam/fileNames/",
                                    dataType : "json",
                                    data:{
                                        name:name
                                    },
                                    success : function(data) {
                                        $("#lookHomework").val(data.result);
                                        $("#lookHomework").show();
                                        console.log(data.result);
                                    },
                                    error : function(xhr,textStatus) {
                                        console.log('错误');
                                        console.log(xhr);
                                        console.log(textStatus);
                                    }
                                });
                            });

                            console.log(data);
                        },
                        error : function(xhr,textStatus) {
                            console.log('错误');
                            console.log(xhr);
                            console.log(textStatus);
                        }
                    });
                });

                $(".searchHomework").click(function(){
                    var groupId=$(this).parent().parent().find("td").eq(0).text();
                    $.ajax({
                        type : "get",
                        url : "/lookTeamExam/searchHomework",
                        dataType : "json",
                        data:{
                          groupId:groupId,
                          date:date
                        },
                        success : function(data) {
                            $("#t2").empty();
                            for(var i in data.names){
                                $("#t2").append("<td ><input type='button' class='lookHomework' value="+data.names[i]+">"+"</td>" );
                            }
                            var len=$("input[name='theRadio']").length;
                            for(var j =0;j<len;j++){
                                if($("input[name='theRadio']:eq("+j+")").val()==data.score){
                                    $("input[name='theRadio']").get(j).checked=true;
                                }
                            }
                            $("#comment").val(data.comment);
                            $(".lookHomework").click(function(){
                                var name=$(this).val();
                                $.ajax({
                                    type : "GET",
                                    url : "/lookTeamExam/fileNames/",
                                    dataType : "json",
                                    data:{
                                        name:name
                                    },
                                    success : function(data) {
                                        $("#lookHomework").val(data.result);
                                        $("#lookHomework").show();
                                        console.log(data.result);
                                    },
                                    error : function(xhr,textStatus) {
                                        console.log('错误');
                                        console.log(xhr);
                                        console.log(textStatus);
                                    }
                                });
                            });
                            console.log(data);
                        },
                        error : function(xhr,textStatus) {
                            console.log('错误');
                            console.log(xhr);
                            console.log(textStatus);
                        }
                    });
                });
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });

    function init(){
     //   $("#tdate").attr("value",$(this).val()); //赋值
        var date=$("#tdate").val();
        $.ajax({
            type : "get",
            url : "/lookTeamExam/commentHomework",
            dataType : "json",
            data:{
                date:date
            },
            success : function(data) {
                $("#winnerId").empty();
                for(var i in data){
                    $("#winnerId").append("<option value=" + data[i].groupId + ">" + data[i].userNames + "</option>");
                    if(data[i].score!=null&&data[i].comment!=null){
                        $("#t1").append("<tr><td>" + data[i].groupId+ "</td>" +
                            "<td>"+data[i].userNames+"</td>" +
                            "<td>"+data[i].spendTime+"</td>" +
                            "<td>"+data[i].score+"</td>" +
                            "<td >\<input type='button' class='searchHomework' value='查看'>\</td></tr>");
                    }else{
                        $("#t1").append("<tr><td>" + data[i].groupId+ "</td>" +
                            "<td>"+data[i].userNames+"</td>" +
                            "<td>"+data[i].spendTime+"</td>" +
                            "<td>"+"未评分"+"</td>" +
                            "<td >\<input type='button' class='correctHomework' value='未批改'>\</td></tr>");
                    }
                }

                $(".correctHomework").click(function(){
                    var groupId=$(this).parent().parent().find("td").eq(0).text();
                    $(this).val("提交");
                    if($(this).val()=="提交"){
                        $(this).click(function(){
                            var comment=$("#comment").val();
                            var date=$("#tdate").val();
                            var radios = document.getElementsByName("theRadio");
                            if(comment==null||comment==""){
                                alert("请填写评论");
                                return;
                            }



                            for (i = 0; i < radios.length; i++) {
                                if (radios[i].checked) {
                                    marks=1;
                                    var radioValue=radios[i].value;
                                    $.ajax({
                                        type : "POST",
                                        url : "/lookTeamExam/uploadResult/",
                                        dataType : "text",
                                        data:{
                                            comment:comment,
                                            date:date,
                                            score:radioValue,
                                            groupId:groupId
                                        },
                                        success : function(data) {
                                            alert("提交成功");
                                            if(1==data){
                                                $(this).val("查看");
                                            }
                                            window.location.href="http://m.zizai.pro/html/lookTeamExam.html";
                                            console.log(data.result);
                                        },
                                        error : function(xhr,textStatus) {
                                            console.log('错误');
                                            console.log(xhr);
                                            console.log(textStatus);
                                        }
                                    });
                                    break;
                                }
                            }
                            if(marks!=1){
                                alert("请打分");
                            }
                        });
                    }

                    $.ajax({
                        type : "get",
                        url : "/lookTeamExam/searchHomework",
                        dataType : "json",
                        data:{
                            groupId:groupId,
                            date:date
                        },
                        success : function(data) {
                            $("#t2").empty();
                            for(var i in data.names){
                                $("#t2").append("<td ><input type='button' class='lookHomework' value="+data.names[i]+">"+"</td>" );
                            }
                            var len=$("input[name='theRadio']").length;
                            for(var j =0;j<len;j++){
                                if($("input[name='theRadio']:eq("+j+")").val()==data.score){
                                    $("input[name='theRadio']").get(j).checked=true;
                                }
                            }
                            $("#comment").val(data.comment);


                            $(".lookHomework").click(function(){
                                var name=$(this).val();
                                $.ajax({
                                    type : "GET",
                                    url : "/lookTeamExam/fileNames/",
                                    dataType : "json",
                                    data:{
                                        name:name
                                    },
                                    success : function(data) {
                                        $("#lookHomework").val(data.result);
                                        $("#lookHomework").show();
                                        console.log(data.result);
                                    },
                                    error : function(xhr,textStatus) {
                                        console.log('错误');
                                        console.log(xhr);
                                        console.log(textStatus);
                                    }
                                });
                            });




                            console.log(data);
                        },
                        error : function(xhr,textStatus) {
                            console.log('错误');
                            console.log(xhr);
                            console.log(textStatus);
                        }
                    });
                });

                $(".searchHomework").click(function(){
                    var groupId=$(this).parent().parent().find("td").eq(0).text();
                    $.ajax({
                        type : "get",
                        url : "/lookTeamExam/searchHomework",
                        dataType : "json",
                        data:{
                            groupId:groupId,
                            date:date
                        },
                        success : function(data) {
                            $("#t2").empty();
                            for(var i in data.names){
                                $("#t2").append("<td ><input type='button' class='lookHomework' value="+data.names[i]+">"+"</td>" );
                            }
                            var len=$("input[name='theRadio']").length;
                            for(var j =0;j<len;j++){
                                if($("input[name='theRadio']:eq("+j+")").val()==data.score){
                                    $("input[name='theRadio']").get(j).checked=true;
                                }
                            }
                            $("#comment").val(data.comment);
                            $(".lookHomework").click(function(){
                                var name=$(this).val();
                                $.ajax({
                                    type : "GET",
                                    url : "/lookTeamExam/fileNames/",
                                    dataType : "json",
                                    data:{
                                        name:name
                                    },
                                    success : function(data) {
                                        $("#lookHomework").val(data.result);
                                        $("#lookHomework").show();
                                        console.log(data.result);
                                    },
                                    error : function(xhr,textStatus) {
                                        console.log('错误');
                                        console.log(xhr);
                                        console.log(textStatus);
                                    }
                                });
                            });
                            console.log(data);
                        },
                        error : function(xhr,textStatus) {
                            console.log('错误');
                            console.log(xhr);
                            console.log(textStatus);
                        }
                    });
                });
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    }
        $("#upload1").click(function(){
            var winnerId=$("#winnerId").val();
            var date=$("#tdate").val();
            $.ajax({
                type : "POST",
                url : "/lookTeamExam/setWinnerId/",
                dataType : "text",
                data:{
                    winnerId:winnerId,
                    date:date
                },
                success : function(data) {
                    alert("提交成功");
                    console.log(data);
                },
                error : function(xhr,textStatus) {
                    console.log('错误');
                    console.log(xhr);
                    console.log(textStatus);
                }
            });
        })
});